package com.cqhilink.iot.jobstandardmanagement.services.innerservice.util;/**
 * Created by Administrator on 2017/4/5.
 */

import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Department;
import com.cqhilink.iot.jobstandardmanagement.facade.model.DepartmentVO;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Organization;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Ztree;

import java.util.Iterator;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 部门Util
 * @CreateDateTime 2017-04-05 17:25
 **/
public class DepartmentUtil {

    /**
     * 全部加载树形信息
     *
     * @param orgList
     * @param depOrgList
     * @param depList
     * @param depItemList
     * @param orgId
     * @param demotree
     */
    public static void orgToTree(List<Organization> orgList, List<Organization> depOrgList, List<Department> depList, List<DepartmentVO> depItemList, String orgId, List<Ztree> demotree) {
        if (depList != null && depList.size() > 0) {
            for (Department dv : depList) {
                if (isDep(dv.getParentOrgId(), orgList, depOrgList)) {
                    continue;
                }
                Ztree demo = new Ztree();
                demo.setId(dv.getDepId());
                demo.setpId(dv.getParentOrgId());
                demo.setName(dv.getDepName());
                demo.setIconSkin(TreeIconSkin.ICON_DEPT.getDesc());
                //判断是否含有下属机构，含有则为true，没有则为false
                demo.setIsParent(false);
                demo.setOpen(false);
                demotree.add(demo);
            }
        }
        if (depOrgList != null && depOrgList.size() > 0) {
            for (Organization org : depOrgList) {
                isContrast(org.getOrgId(), orgList);
                demotree.add(entity(org, orgId));
            }
            if (orgList != null && orgList.size() > 0) {
                for (Organization org : orgList) {
                    demotree.add(entity(org, orgId));
                }
            }
        } else {
            for (Organization org : orgList) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                boolean isParent = isDep(org.getOrgId(), depItemList);
                demo.setIsParent(isParent);
                if (org.getOrgId().equals(orgId))
                    demo.setOpen(true);
                else
                    demo.setOpen(false);

                demotree.add(demo);
            }
        }
    }

    /**
     * 异步树形组装
     *
     * @param orgList
     * @param itemList
     * @param depList
     * @param depItemList
     * @param orgId
     * @param demotree
     */
    public static void depAndOrgToTree(List<Organization> orgList, List<Organization> itemList, List<Department> depList, List<DepartmentVO> depItemList, String orgId, List<Ztree> demotree) {
        if (depList != null && depList.size() > 0) {
            for (Department dv : depList) {
                Ztree demo = new Ztree();
                demo.setId(dv.getDepId());
                demo.setpId(dv.getParentOrgId());
                demo.setName(dv.getDepName());
                demo.setIconSkin(TreeIconSkin.ICON_DEPT.getDesc());
                //判断是否含有下属机构，含有则为true，没有则为false
                demo.setOpen(false);
                demotree.add(demo);
            }
        }
        for (Organization org : orgList) {
            Ztree demo = new Ztree();
            demo.setId(org.getOrgId());
            demo.setpId(org.getParentOrgId());
            demo.setName(org.getOrgName());
            demo.setOrgLevel(org.getOrgLevelId());
            int levelId = Integer.parseInt(org.getOrgLevelId());
            demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
            //判断是否含有下属机构，含有则为true，没有则为false
            boolean isParent = isOrg(org.getOrgId(), depItemList, itemList);
            demo.setIsParent(isParent);
            if (org.getOrgId().equals(orgId))
                demo.setOpen(true);
            else
                demo.setOpen(false);
            demotree.add(demo);
        }
    }

    /**
     * 异步模糊搜索树形组装
     *
     * @param orgList
     * @param depList
     * @param demotree
     */
    public static void depAndOrgToTreeTrue(List<Organization> orgList, List<Department> depList, List<Ztree> demotree) {
        if (depList != null && depList.size() > 0) {
            for (Department dv : depList) {
                Ztree demo = new Ztree();
                demo.setId(dv.getDepId());
                demo.setpId(dv.getParentOrgId());
                demo.setName(dv.getDepName());
                demo.setIconSkin(TreeIconSkin.ICON_DEPT.getDesc());
                //判断是否含有下属机构，含有则为true，没有则为false
                demotree.add(demo);
            }
        }
        for (Organization org : orgList) {
            Ztree demo = new Ztree();
            demo.setId(org.getOrgId());
            demo.setpId(org.getParentOrgId());
            demo.setName(org.getOrgName());
            demo.setOrgLevel(org.getOrgLevelId());
            int levelId = Integer.parseInt(org.getOrgLevelId());
            demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
            //判断是否含有下属机构，含有则为true，没有则为false
            demo.setOpen(true);
            demotree.add(demo);
        }
    }

    /**
     * 组装组织数据
     *
     * @param org
     * @param orgId
     * @return
     */
    private static Ztree entity(Organization org, String orgId) {
        Ztree demo = new Ztree();
        demo.setId(org.getOrgId());
        demo.setpId(org.getParentOrgId());
        demo.setName(org.getOrgName());
        demo.setOrgLevel(org.getOrgLevelId());
        int levelId = Integer.parseInt(org.getOrgLevelId());
        demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
        //判断是否含有下属机构，含有则为true，没有则为false
        demo.setIsParent(false);
        if (org.getOrgId().equals(orgId))
            demo.setOpen(true);
        else
            demo.setOpen(false);
        return demo;
    }

    /**
     * 验证是否存在的组织部门
     *
     * @param orgId
     * @param orgList
     * @param orgList2
     * @return
     */
    public static boolean isDep(String orgId, List<Organization> orgList, List<Organization> orgList2) {
        if (orgList != null && orgList.size() > 0) {
            for (Organization org : orgList) {
                if (orgId.equals(org.getOrgId())) {
                    return false;
                }
            }
        }
        if (orgList2 != null && orgList2.size() > 0) {
            for (Organization org : orgList2) {
                if (orgId.equals(org.getOrgId())) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 判断当前数据下面是否有子项信息
     *
     * @param orgId
     * @return 有就返回true，没有返回false
     */
    private static boolean isDep(String orgId, List<DepartmentVO> depList) {
        if (depList != null) {
            Iterator<DepartmentVO> depIt = depList.iterator();
            while (depIt.hasNext()) {
                DepartmentVO param = depIt.next();
                if (param.getParentOrgId().equals(orgId)) {
                    depIt.remove();
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断当前数据下面是否有子项信息
     *
     * @param orgId
     * @return 有就返回true，没有返回false
     */
    private static boolean isOrg(String orgId, List<DepartmentVO> depList, List<Organization> orgList) {
        if (orgList != null) {
            Iterator<Organization> it = orgList.iterator();
            while (it.hasNext()) {
                Organization param = it.next();
                if (param.getParentOrgId().equals(orgId)) {
                    it.remove();
                    return true;
                }
            }
        }
        return isDep(orgId, depList);
    }

    /**
     * 判断是否相同数据
     *
     * @param orgId
     * @param orgList
     * @return
     */
    private static void isContrast(String orgId, List<Organization> orgList) {
        if (orgList != null && orgList.size() > 0) {
            Iterator<Organization> it = orgList.iterator();
            while (it.hasNext()) {
                Organization param = it.next();
                if (param.getOrgId().equals(orgId)) {
                    it.remove();
                }
            }
        }
    }

}
